require "Common/define"
require "Logic/Config"
require "Common/functions"

HeroHBBItem = {};
HelperFunc.SetUIEnv(HeroHBBItem)
local this = HeroHBBItem;

this.gameObject = nil;
this.transform = nil;
this.LuaBehaviour = nil;
this.img_Icon = nil;
this.img_Type = nil;
this.obj_Mask = nil;
this.txt_Level = nil;
this.obj_Star = nil;
this.tab_Star = nil;
this.obj_Selected = nil;

local hasInit = false;

function HeroHBBItem.Init()
    local name,package = getUIName(PanelNames.HeroHBBItem);
    panelMgr:ShowUI(name,package, this.onCreate,Config.useLocalUI);
end

function HeroHBBItem.onCreate(obj)
    obj:SetActive(false);
end
---@param obj FairyGUI.GComponent
function HeroHBBItem.initData(obj,data,luaBehaviour)
    --LuaBehaviour = luaBehaviour;
    --gameObject = obj;
    --transform = obj.transform;
    
    img_Icon = obj:GetChild("IconMask")
    img_Icon = img_Icon:GetChild("icon")
    local name = obj:GetChild("name")
    txt_Level = obj:GetChild("Level")
    img_Type = obj:GetChild("Type")
    obj_Mask = obj:GetChild("mask");
    --obj_Star = obj:GetChild("Star")
    tab_Star = {};
    for i =0, 5 do
        local star = obj:GetChild("star"..i);
        table.insert(tab_Star, star);
    end
    obj_Selected = obj:GetChild("Selected");

    obj_Mask.visible = false
    img_Icon.visible = false
    img_Type.visible = false
    txt_Level.visible = false
    --obj_Star.visible = false
    obj_Selected.visible = false

    if data ~= nil then
        if data["name"] ~= nil then
            name.visible = true;
            name.text = data["name"];
        else
            name.visible = false;
        end

        if data["level"] ~= nil then
            txt_Level.visible = true;
            txt_Level.text = "Lv"..data["level"];
        end

        if data["star"] ~= nil then
            --obj_Star.visible = true;
            local count = data["star"];
            for i = 1, 6 do
                local visible = i <= count
                --tab_Star[i].visible = visible;
                local ctrl = tab_Star[i]:GetController("ctrlStar")
                ctrl:SetSelectedIndex(visible and 1 or 0)
            end
        end

        if data["partnerModelId"] ~= nil then
            local icon = data.icon;
            local type = data.type;
            if icon ~= nil then
                img_Icon.visible = true
                img_Icon.url = "ui://HalfBodyAtlas/"..icon
                --resMgr:LoadIconFromPrefab("halfBodyAtlas", icon,function(sp)
                --    img_Icon.gameObject:SetActive(true);
                --    img_Icon.sprite = sp;
                --end);
            end
            if type ~= nil then
                img_Type.visible = true
                img_Type.url = "ui://ICONAtlas/"..EnumConst.HeroTypeIcon[type];
                --resMgr:LoadIconFromPrefab("ICONAtlas", tab_Type[type],function(sp)
                --    img_Type.gameObject:SetActive(true);
                --    img_Type.sprite = sp;
                --end);
            end
        end

        if data["selected"] ~= nil and data["selected"] then
            obj_Selected.visible = true
        else
            obj_Selected.visible = false
        end
    end
end

function HeroHBBItem.setSelected(obj,value)
    print(value);
    local obj_Selected = obj:GetChild("Selected");
    obj_Selected.visible = value
end


return HeroHBBItem;